<template>
	<div class="main">
		<div class="message">
			<img src="@/assets/images/wechat/index/authorizelogin/authorize-bg.png">
			<div class="message_content">
				<div class="message_title">{{messageData.title}}</div>
				<div class="message_desc">{{messageData.desc}}</div>
			</div>
		</div>
		<div class="link">
			<a href="javascript:;">返回首页</a>
		</div>
	</div>
</template>

<script>
//引入js	
import router from '@/router/index'
import { loginByOpenid } from '@/api/lawyer'

export default {
	components: {
	},
    data(){
		return {
			baseUrl:process.env.VUE_APP_BASE_API,
			appId:"",
			
			messageData:{title:'授权登录中',desc:'该类功能需要在个人中心授权后才能使用'},
		}
    },
    created:function(){
		var that=this;
		//获取appid
		that.appId=that.$store.state.globalData.wechatConfig.appId
		//获取url地址
		var url = window.location.href;
		//获取code
		var code=that.getQueryString("code");
		//code不存在则开始授权处理
		if(!code)
		{
			window.location="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+that.appId+"&redirect_uri="+encodeURIComponent(url)+"&response_type=code&scope=snsapi_userinfo#wechat_redirect";
			return 0;
		}
		//取得code，ajax获取用户信息并实现登录
		that.login(code,url);
			  
  	},
  	beforeMount: function() {
		
    },
  	mounted: function () {
  		
  	},
  	methods:{
  		// 获取地址栏参数
  		getQueryString:function(name) {
  			let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  			let url = window.location.search.substr(1).match(reg);
  			return url ? decodeURI(url[2]) : null;
  		},
		login:async function(code,url){
			//拿到token
			var ajaxData={}
			ajaxData.code=code
			var resultData=await loginByOpenid(ajaxData);
			
			if(resultData.code==200)
			{
				//存起来token和身份信息
				var expireTime=Date.parse(new Date())/1000+72000;//20小时后过期
				//同步更新store
				this.$store.commit("setIdentity",6);
				this.$store.commit("setToken",resultData.data);
				this.$store.commit("setExpireTime",expireTime);
				 /*存储到ls*/
				localStorage.setItem('identity',6);
				localStorage.setItem('token',resultData.data);
				localStorage.setItem('expireTime',expireTime);
				
				//跳转到指定页面
				url=url.split('?')[2];
				if(!url){
					router.replace({path:"/wechatlawyer/lawyercenter"});
					return 0;
				}
				var action=url.split('&')[0];
				if(!action){
					router.replace({path:"/wechatlawyer/lawyercenter"});
					return 0;
				}
				action=action.replace('action=','');
				router.replace({path:action});
				return 0;
			}
			else{
				this.messageData.title="登录失败"
				this.messageData.desc="在设置中绑定微信后才能使用微信登录"
			}
		}
  	}
}
</script>

<style lang="scss" scoped>
.main{
     width:100%;
     overflow:hidden;
     position:relative;
}
.message{ 
	width: 100%; 
	position: relative;
}
.message>img{ 
	width: 100%; 
	display: block;
}
.message_content{ 
	width: 100%; 
	position: absolute; 
	left: 0; 
	bottom:5rem;
}
.message_title{ 
	width: 100%; 
	font-size: 1.125rem; 
	color: #FFFFFF; 
	text-align: center;
}
.message_desc{ 
	width: 100%; 
	color: #FFFFFF; 
	text-align: center; 
	margin-top: 1.25rem;
}

.link{ 
	width: 100%; 
	margin-top: 1.875rem;
}
.link>a{ 
	display: block; 
	width: 7.5rem; 
	line-height: 2rem; 
	margin: auto; 
	text-align: center; 
	border: 1px solid #007AFF; 
	color: #007AFF; 
	border-radius: 0.25rem;
}

</style>
